namespace com.stifel.ServerBuildMgmt.Features.ServerBuildMgmt_Feature
{
    using System;
    using System.Runtime.InteropServices;

    using com.stifel.Exceptions.SharePointExceptions;
    using com.stifel.Extensions.SharePointExtensions;
    using com.stifel.SharePoint;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;

    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid( "6bd0a2d2-7389-40f2-a916-a25d23da441e" )]
    public class FeatureEventReceiver : SPFeatureReceiver
    {
        #region Fields

        static string _title;

        #endregion Fields

        #region Properties

        public static Guid ID
        {
            get
            {
                return new Guid( "24271fa8-6881-4638-8cac-82472a1d704c" ); // from feature manifest
            }
        }

        public static string Title
        {
            get
            {
                if ( string.IsNullOrEmpty( _title ) )
                    _title = SPFarm.Local.GetFeatureDefinition( ID ).GetTitle( );
                return _title;
            }
        }

        #endregion Properties

        #region Methods

        public override void FeatureActivated( SPFeatureReceiverProperties properties )
        {
            try
            {
                if ( !( properties.Feature.Parent is SPWeb ) )
                {
                    throw new FeatureActivationFailedNotScopedForWebException( ID, Title, properties.GetWebUrl( ) );
                }

                CustomLoggingService.LogMessage( CustomLoggingService.Area.FeatureInfrastructure, "{0} :: Feature activating.", Title );

                using ( StifelUserType_Definition o = new StifelUserType_Definition( properties ) )
                    o.Create( );

                using ( StifelOSRole_Definition o = new StifelOSRole_Definition( properties ) )
                    o.Create( );

                using ( StifelEnvirons_Definition o = new StifelEnvirons_Definition( properties ) )
                    o.Create( );

                using ( StifelDRReplicationType_Definition o = new StifelDRReplicationType_Definition( properties ) )
                    o.Create( );

                using ( StifelVirtualServerLocation_Definition o = new StifelVirtualServerLocation_Definition( properties ) )
                    o.Create( );

                using ( StifelVLANCluster_Definition o = new StifelVLANCluster_Definition( properties ) )
                    o.Create( );

                using ( StifelVLAN_Definition o = new StifelVLAN_Definition( properties ) )
                    o.Create( );

                using ( StifelNetworkConnectionType_Definition o = new StifelNetworkConnectionType_Definition( properties ) )
                    o.Create( );

                using ( StifelOS_Definition o = new StifelOS_Definition( properties ) )
                    o.Create( );

                using ( StifelVirtualServerRequest_Definition o = new StifelVirtualServerRequest_Definition( properties ) )
                    o.Create( );

                using ( StifelAVPolicy_Definition o = new StifelAVPolicy_Definition( properties ) )
                    o.Create( );

                using ( StifelBackupAgent_Definition o = new StifelBackupAgent_Definition( properties ) )
                    o.Create( );

                using ( StifelBackupHour_Definition o = new StifelBackupHour_Definition( properties ) )
                    o.Create( );

                using ( StifelBackupPolicy_Definition o = new StifelBackupPolicy_Definition( properties ) )
                    o.Create( );

                using ( StifelBackupRetention_Definition o = new StifelBackupRetention_Definition( properties ) )
                    o.Create( );

                using ( StifelBuildCheckList_Definition o = new StifelBuildCheckList_Definition( properties ) )
                    o.Create( );

                CustomLoggingService.LogMessage( CustomLoggingService.Area.FeatureInfrastructure, "{0} :: Feature activated.", Title );
            }
            catch ( Exception e )
            {
                throw new FeatureActivationException( ID, Title, properties.GetWebUrl( ), e );
            }
        }

        public override void FeatureDeactivating( SPFeatureReceiverProperties properties )
        {
            try
            {
                if ( !( properties.Feature.Parent is SPWeb ) )
                {
                    throw new FeatureDeactivationFailedNotScopedForWebException( ID, Title, properties.GetWebUrl( ) );
                }

                CustomLoggingService.LogMessage( CustomLoggingService.Area.FeatureInfrastructure, "{0} :: Feature deactivating.", Title );

                using ( StifelVirtualServerRequest_Definition o = new StifelVirtualServerRequest_Definition( properties ) )
                    o.Delete( );

                using ( StifelOS_Definition o = new StifelOS_Definition( properties ) )
                    o.Delete( );

                using ( StifelNetworkConnectionType_Definition o = new StifelNetworkConnectionType_Definition( properties ) )
                    o.Delete( );

                using ( StifelVLAN_Definition o = new StifelVLAN_Definition( properties ) )
                    o.Delete( );

                using ( StifelVLANCluster_Definition o = new StifelVLANCluster_Definition( properties ) )
                    o.Delete( );

                using ( StifelVirtualServerLocation_Definition o = new StifelVirtualServerLocation_Definition( properties ) )
                    o.Delete( );

                using ( StifelDRReplicationType_Definition o = new StifelDRReplicationType_Definition( properties ) )
                    o.Delete( );

                using ( StifelEnvirons_Definition o = new StifelEnvirons_Definition( properties ) )
                    o.Delete( );

                using ( StifelOSRole_Definition o = new StifelOSRole_Definition( properties ) )
                    o.Delete( );

                using ( StifelUserType_Definition o = new StifelUserType_Definition( properties ) )
                    o.Delete( );

                CustomLoggingService.LogMessage( CustomLoggingService.Area.FeatureInfrastructure, "{0} :: Feature deactivated.", Title );
            }
            catch ( Exception e )
            {
                throw new FeatureDeactivationException( ID, Title, properties.GetWebUrl( ), e );
            }
        }

        #endregion Methods
    }
}